package de.fuberlin.inf.alp3.ue01;

import javax.management.RuntimeErrorException;

public class Riddle {
    public static void main(String[] args) {
        int[] row0 = new int[5];
        int[] row1 = new int[3];
        int[] row2 = new int[4];
        int[] row3 = new int[2];
        int[] row4 = new int[5];
        int[][] x = {row0, row1, row2, row3, row4};
        
        x[0][0] = 1; x[0][1] = 2; x[0][2] = 3; x[0][3] = 5; x[0][4] = 6;
        x[1][0] = 3; x[1][1] = 5; x[1][2] = 6;
        x[2][0] = 1; x[2][1] = 2; x[2][2] = 3; x[2][3] = 4;
        x[3][0] = 2; x[3][1] = 3;
        x[4][0] = 3; x[4][1] = 4; x[4][2] = 5; x[4][3] = 8; x[4][3] = 9;
        
        System.out.println(riddle(x));
    }
    private static int riddle(int[][] table){
        if (table.length == 0 || table[0].length == 0)
            throw new RuntimeErrorException(new Error("keine Elemente"));
        int min;
        for (int i = 0; i < table[0].length; i++) {
            min = table[0][i];
            boolean isInAllRows = true;
            for (int j = 1; j < table.length; j++)
                if (!isInRow(table[j], min)) {
                    isInAllRows = false;
                    break;
                }
            if (isInAllRows)
                return min;
        }
        throw new RuntimeErrorException(new Error("keine Element, das in " +
        		"allen Listen enthalten ist"));
    }
    private static boolean isInRow(int[] row, int x) {
        for (int i = 0; i < row.length; i++)
            if (row[i] == x)
                return true;
            else if (row[i] > x)
                return false;
        return false;
    }
}